package com.jingtai.platform.service.shop.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingtai.common.enums.DeleteFlagEnum;
import com.jingtai.common.template.ResultMapInfo;
import com.jingtai.common.utils.random.TokenUtil;
import com.jingtai.core.entity.shop.MerchantsGoodsWarehouse;
import com.jingtai.core.mapper.shop.MerchantsGoodsWarehouseMapper;
import com.jingtai.platform.service.shop.GoodsSpecificationsService;
import com.jingtai.platform.service.shop.MerchantsGoodsWarehouseService;
import com.jingtai.platform.util.ResultUtil;
import com.jingtai.platform.util.UrlToMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


/**
 * 商户商品属性表 impl
 *
 * @author sunlaiqian
 * @date 2020-12-02 18:31:32
 */
@Service
public class MerchantsGoodsWarehouseServiceImpl extends ServiceImpl<MerchantsGoodsWarehouseMapper, MerchantsGoodsWarehouse> implements MerchantsGoodsWarehouseService {

    @Autowired
    private MerchantsGoodsWarehouseService merchantsGoodsWarehouseService;
    @Autowired
    private GoodsSpecificationsService goodsSpecificationsService;

    @Override
    public ResultUtil MerchantsGoodsWarehouselist(Integer page, Integer limit, String conditions){
        QueryWrapper<MerchantsGoodsWarehouse> wrapper = new QueryWrapper<>();
        Map<String, String> map = UrlToMap.paramToMap(conditions);
        for(Map.Entry<String, String> entry : map.entrySet()){
            wrapper.like(entry.getKey(), entry.getValue());
        }
        wrapper.eq("delete_flag", DeleteFlagEnum.NOT_DELETE.getCode());
        return new ResultUtil(ResultMapInfo.GETSUCCESS,merchantsGoodsWarehouseService.page(new Page<>(page,limit),wrapper));
    }

    @Override
    public ResultUtil addMerchantsGoodsWarehouse(MerchantsGoodsWarehouse merchantsGoodsWarehouse) {
        merchantsGoodsWarehouse.setSkuCode(TokenUtil.generateOrderNo());
        //格式化 属性json
        merchantsGoodsWarehouse.setSpecificationsJson(goodsSpecificationsService.formatJson(merchantsGoodsWarehouse.getSpecificationsJson(), merchantsGoodsWarehouse.getGoodsCode(),1));
        boolean save = merchantsGoodsWarehouseService.save(merchantsGoodsWarehouse);
        return new ResultUtil(save ? ResultMapInfo.ADDSUCCESS:ResultMapInfo.ADDFAIL);
    }

    @Override
    public ResultUtil getMerchantsGoodsWarehouseById(Long id) {
        MerchantsGoodsWarehouse merchantsGoodsWarehouse = merchantsGoodsWarehouseService.getById(id);
        return merchantsGoodsWarehouse == null ?  new ResultUtil(ResultMapInfo.GETFAIL) : new ResultUtil(ResultMapInfo.GETSUCCESS,merchantsGoodsWarehouse);
    }

    @Override
    public ResultUtil delMerchantsGoodsWarehouseById(Long id) {
        MerchantsGoodsWarehouse merchantsGoodsWarehouse = new MerchantsGoodsWarehouse();
        merchantsGoodsWarehouse.setId(id);
        merchantsGoodsWarehouse.setDeleteFlag(DeleteFlagEnum.DELETE.getCode());
        boolean delState = merchantsGoodsWarehouseService.updateById(merchantsGoodsWarehouse);
        return delState ?  new ResultUtil(ResultMapInfo.DELETESUCCESS) : new ResultUtil(ResultMapInfo.DELETEFAIL);
    }

    @Override
    public ResultUtil editMerchantsGoodsWarehouse(MerchantsGoodsWarehouse merchantsGoodsWarehouse) {
        merchantsGoodsWarehouse.setSpecificationsJson(goodsSpecificationsService.formatJson(merchantsGoodsWarehouse.getSpecificationsJson(), merchantsGoodsWarehouse.getGoodsCode(),1));
        boolean editState = merchantsGoodsWarehouseService.updateById(merchantsGoodsWarehouse);
        return editState ?  new ResultUtil(ResultMapInfo.EDITSUCCESS) : new ResultUtil(ResultMapInfo.EDITFAIL);
    }

}
